/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.material.excel;

import lombok.RequiredArgsConstructor;
import org.springblade.common.utils.CommonUtil;
import org.springblade.core.excel.support.ExcelImporter;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.material.entity.AdmissionTicketEntity;
import org.springblade.modules.material.entity.ElectronicInformationEntity;
import org.springblade.modules.material.service.IAdmissionTicketService;
import org.springblade.modules.material.service.IElectronicInformationService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 题目数据导入类
 *
 * @author junxin
 */
@RequiredArgsConstructor
public class AdmissionTicketImporter implements ExcelImporter<AdmissionTicketExcel> {
	private final IAdmissionTicketService admissionTicketService;
	private final IElectronicInformationService electronicInformationService;

	@Override
	@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
	public void save(List<AdmissionTicketExcel> dataList) {
		if (dataList.size()>4000)throw new ServiceException("导入条数不可超过4000条");
		String version= CommonUtil.getImportVersion();
		int size = dataList.size();
		List<AdmissionTicketEntity> list=new ArrayList<>();

		for (int i = 0; i <size ; i++) {
			AdmissionTicketExcel data = dataList.get(i);
			paramsVerification(data,i);

			AdmissionTicketEntity entity=new AdmissionTicketEntity();
			BeanUtil.copyProperties(data,entity);
			//出生年月
			String bornMonth=data.getIdCard().substring(7,15);
			entity.setBornMonth(bornMonth);

			//性别
			String sex = data.getIdCard().substring(17);
			int sexNumber = Integer.parseInt(sex);
			if (sexNumber%2==0) entity.setSex("男");
			if (sexNumber%2==1) entity.setSex("女");

			if (Func.isEmpty(data.getVersion())) entity.setVersion(version);
			//用户电子信息
			ElectronicInformationEntity electronicInformationEntity = electronicInformationService.getByIdcard(data.getIdCard());
			if (null!=electronicInformationEntity && Func.isNotEmpty(electronicInformationEntity.getUserImg()))entity.setUserImg(electronicInformationEntity.getUserImg());
			list.add(entity);
		}

		admissionTicketService.saveBatch(list);
	}


	/**
	 * 导入的规则校验
	 * admissionTicketExcel 为参数
	 * i为序号
	 */
	public void paramsVerification(AdmissionTicketExcel admissionTicketExcel,int i){
		if (Func.isEmpty( admissionTicketExcel.getIdCard())) throw new ServiceException("序号:"+(i+2)+"的身份证编码为空，请补充完成再重新上传!"  );
		if ( admissionTicketExcel.getIdCard().length()!=18 ) throw new ServiceException("序号:"+(i+2)+"的身份证编码有误，请修改完成后再重新上传!"  );
		boolean numeric = Func.isNumeric(admissionTicketExcel.getIdCard().substring(17));
		if (!numeric)throw new ServiceException("序号:"+(i+2)+"的身份证编码有误，请修改完成后再重新上传!"  );
	}


}
